﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using PassBag.Logic.Crypt;

namespace PassBag.Logic {
    /// <summary>
    /// Help to encrypt <see cref="PasswordObject"/> object to <see cref="PasswordCryptObject"/> object.
    /// </summary>
    public class EncryptPasswordObject {
        /// <summary>
        /// Encrypts the specified password object.
        /// </summary>
        /// <param name="passwordObject">The password object.</param>
        /// <param name="password">The password.</param>
        /// <returns>The encrypted form of <see cref="PasswordObject"/> object is the <see cref="PasswordCryptObject"/> object.</returns>
        public static PasswordCryptObject Encrypt (PasswordObject passwordObject, string password) {
            EncryptString crypt = new EncryptString (password);
            PasswordCryptObject outPasswordObject = new PasswordCryptObject ();

            outPasswordObject.XmlId = crypt.EncryptStringToHexString (passwordObject.Id.ToString ());
            outPasswordObject.Information = crypt.EncryptStringToHexString (passwordObject.Information);
            outPasswordObject.Title = crypt.EncryptStringToHexString (passwordObject.Title);
            outPasswordObject.UserName = crypt.EncryptStringToHexString (passwordObject.UserName);
            outPasswordObject.Password = crypt.EncryptStringToHexString (passwordObject.Password);
            outPasswordObject.Url = crypt.EncryptStringToHexString (passwordObject.Url);
            outPasswordObject.Comment = crypt.EncryptStringToHexString (passwordObject.Comment);
            outPasswordObject.XmlLastModified = crypt.EncryptStringToHexString (passwordObject.LastModified.ToString ());

            return outPasswordObject;
        }

        /// <summary>
        /// Decrypts the specified <see cref="PasswordCryptObject"/> to an <see cref="PasswordObject"/>.
        /// </summary>
        /// <param name="passwordObject">The password object.</param>
        /// <param name="password">The password.</param>
        /// <returns>The decrypted form of <see cref="PasswordCryptObject"/> object is the <see cref="PasswordObject"/> object.</returns>
        public static PasswordObject Decrypt (PasswordCryptObject passwordObject, string password) {
            EncryptString crypt = new EncryptString (password);
            PasswordObject outPasswordObject = new PasswordObject ();

            outPasswordObject.Id = new Guid (crypt.DecryptHexStringToString (passwordObject.XmlId.ToString ()));
            outPasswordObject.Information = crypt.DecryptHexStringToString (passwordObject.Information);
            outPasswordObject.Title = crypt.DecryptHexStringToString (passwordObject.Title);
            outPasswordObject.UserName = crypt.DecryptHexStringToString (passwordObject.UserName);
            outPasswordObject.Password = crypt.DecryptHexStringToString (passwordObject.Password);
            outPasswordObject.Url = crypt.DecryptHexStringToString (passwordObject.Url);
            outPasswordObject.Comment = crypt.DecryptHexStringToString (passwordObject.Comment);
            outPasswordObject.LastModified = DateTime.Parse (crypt.DecryptHexStringToString (passwordObject.XmlLastModified.ToString ()));

            return outPasswordObject;
        }
    }
}
